package org.ccahouse.ccacommons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    /**
     * 获取今天开始时间
     * @return
     */
    public static String getTodayStartDate() {
        Calendar calendar = Calendar.getInstance();
        setStartDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取今天结束时间
     * @return
     */
    public static String getTodayEndDate() {
        Calendar calendar = Calendar.getInstance();
        setEndDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取昨天开始时间
     * @return
     */
    public static String getYesTerdayStartDate() {
        Calendar calendar = Calendar.getInstance();
        setStartDate(calendar);
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取昨天结束时间
     * @return
     */
    public static String getYesTerdayEndDate() {
        Calendar calendar = Calendar.getInstance();
        setEndDate(calendar);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本周开始时间
     * @return
     */
    public static String getWeekStartDate() {
        Calendar calendar = Calendar.getInstance();
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek);
        setStartDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本周结束时间
     * @return
     */
    public static String getWeekEndDate() {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getWeekStartDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        setEndDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }


    /**
     * 获取本月开始时间
     * @return
     */
    public static String getMonthStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        setStartDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本月结束时间
     * @return
     */
    public static String getMonthEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        setEndDate(calendar);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取传入时间的当天起始时间
     * @param date
     * @return
     */
    public static Date getStartTimeOfDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        setStartDate(calendar);
        return calendar.getTime();
    }

    /**
     * 获取传入时间的当天结束时间
     * @param date
     * @return
     */
    public static Date getEndTimeOfDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        setEndDate(calendar);
        return calendar.getTime();
    }
    /**
     * 设置当天开始时间
     * @param calendar
     */
    private static void setStartDate(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
    }

    /**
     * 设置当天结束时间
     * @param calendar
     */
    private static void setEndDate(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
    }

    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static Integer getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    public static void main(String[] args) {
        System.out.println(getMonthStartDate());
        System.out.println(getMonthEndDate());
    }
}
